/*******************************************************************************
 * Copyright (C) 2010 Devin Samarin.
 * 
 * This file is part of the SUIT Toolkit.
 * 
 * The SUIT Toolkit is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * The SUIT Toolkit is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public License
 * along with the SUIT Toolkit.  If not, see <http://www.gnu.org/licenses/>.
 ******************************************************************************/
import java.io.DataOutputStream;
import java.io.IOException;
import java.util.Date;

import javax.microedition.io.ConnectionNotFoundException;
import javax.microedition.io.Connector;
import javax.microedition.io.file.FileConnection;

import suit.image.BMP;
import suit.image.ImageListener;
import suit.ui.Application;
import suit.ui.Box;
import suit.ui.Button;
import suit.ui.Label;
import suit.ui.ProgressBar;
import suit.ui.Screen;
import suit.ui.ToggleButton;
import suit.ui.themes.DefaultTheme;

public class Demo extends Application {

	private Screen main;
	private Box box;
	private Label status;
	private ProgressBar progress;
	private Box buttons;
	private Button increase;
	private Button decrease;
	private ToggleButton toggle;
	private Button update;
	private Button snap;

	public Demo() {

		main = new Screen(new DefaultTheme());
		box = new Box(Box.Vert, false, 6);
		box.setBorderWidth(6);

		status = new Label("SUIT Demo");
		progress = new ProgressBar("Not really loading...");
		buttons = new Box(Box.Horiz, true, 6);

		increase = new Button("Increase");
		increase.connect(new Button.Clicked() {
			public void onClicked(Button source) {
				progress.setFraction(progress.getFraction() + 0.05);
			}
		});
		decrease = new Button("Decrease");
		decrease.connect(new Button.Clicked() {
			public void onClicked(Button source) {
				progress.setFraction(progress.getFraction() - 0.05);
			}
		});
		toggle = new ToggleButton("Toggle Fullscreen");
		toggle.connect(new ToggleButton.Toggled() {
			public void onToggled(ToggleButton source) {
				main.setFullScreenMode(source.getActive());
			}
		});

		buttons.pack(decrease, true, true, 0);
		buttons.pack(toggle, true, true, 0);
		buttons.pack(increase, true, true, 0);

		update = new Button("Upgrade");

		box.pack(status, true, true, 0);
		box.pack(progress, false, true, 0);
		box.pack(buttons, true, true, 0);
		box.pack(update, true, true, 0);

		snap = new Button("Snap");
		snap.connect(new Button.Clicked() {
			
			public void onClicked(Button source) {
				saveScreenshot();
			}
		});
		box.pack(snap, false, true, 0);

		main.add(box);
		setScreen(main);

		update.connect(new Button.Clicked() {

			public void onClicked(Button source) {
				try {
					platformRequest("http://eboyjr.ath.cx:4445/SUIT%20Toolkit/deployed/QwertyDevice/SUIT_Demo.jad");
				} catch (ConnectionNotFoundException e) {
					status.setText("Failed!");
				}

			}
		});
	}

	public class ScreenshotSaver implements Runnable, ImageListener {

		public void run() {
			try {
				FileConnection filecon = (FileConnection) Connector
						.open("file:///memorycard/Screenshots/screenshot"
								+ (new Date()).getTime() + ".bmp");
				if (!filecon.exists()) {
					filecon.create();
				}
				DataOutputStream stream = filecon.openDataOutputStream();
				BMP screenshot = new BMP(main.getScreenshot());
				screenshot.connect(this);
				screenshot.encode(stream); 
				stream.close();
				filecon.close();
			} catch (IOException ioe) {
			}
		}

		public void onComplete() {
			progress.setText("Done!");
		}

		public void onProgress(double fraction) {
			progress.setFraction(fraction);
		}

		public void onStart() {
			progress.setText("Creating screenshot...");
		}
	}

	public void saveScreenshot() {

		new Thread(new ScreenshotSaver()).start();
	}

	public boolean onKeyReleased(int key) {
		if (key == -8) {
			notifyDestroyed();
		} else if (key == -103 || key == -7) {
			saveScreenshot();
		}
		return true;
	}

}
